package com.dalei.vds;

import com.dalei.vds.settings.ImageSettings;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ImageSettingsActivity extends Activity {

	private EditText scaleEdit;

	private Button saveBtn;

	private Button cancelBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagesettings);

		scaleEdit = (EditText) findViewById(R.id.imagesettings_scale);
		ImageSettings s = new ImageSettings(this);
		int percentValue = (int) (s.getScale() * 100);
		scaleEdit.setText(Integer.toString(percentValue));
		scaleEdit
				.setFilters(new InputFilter[] { new InputFilter.LengthFilter(3) });
		scaleEdit.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				saveBtn.setEnabled(true);
			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (s.length() != 0) {
					int value = Integer.parseInt(s.toString());
					if ((value < 1) || (value > 100)) {
						Toast.makeText(ImageSettingsActivity.this,
								getString(R.string.scale_err_msg),
								Toast.LENGTH_SHORT).show();
						if (value < 1) {
							scaleEdit.setText("1");
						}
						if (value > 100) {
							scaleEdit.setText("100");
						}
					}
				} else {
					saveBtn.setEnabled(false);
				}
			}
		});

		saveBtn = (Button) findViewById(R.id.imagesettings_btn_save);
		saveBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				int value = Integer.parseInt(scaleEdit.getText().toString());
				if (value > 0 && value <= 100) {
					ImageSettings settings = new ImageSettings(
							ImageSettingsActivity.this);
					double scale = ((double) Integer.parseInt(scaleEdit
							.getText().toString())) / 100;
					settings.setScale(scale);
					finish();
				} else {
					Toast.makeText(ImageSettingsActivity.this,
							getString(R.string.scale_err_msg),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		cancelBtn = (Button) findViewById(R.id.imagesettings_btn_cancel);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
}
